<script lang="ts" setup>
import mcht from '@/api/mcht'

interface Option {
  text: string
  value: string
}

const businessStatusOptions = ref<Option[]>([
  { text: '有固定经营场所', value: '0' },
  { text: '无固定经营场所', value: '1' },
])

const idTypeOptions = ref<Option[]>([
  { text: '身份证', value: '01' },
])
const shopStatusOptions = ref<Option[]>([
  { text: '正常', value: '0' },
  { text: '注销', value: '2' },
  { text: '新增', value: '3' },
  { text: '核查', value: '4' },
  { text: '拒绝', value: '5' },
])

const feeRateInfoColumns = [
  {
    title: '产品',
    key: 'productName',
    align: 'center',
  },
  {
    title: '执行费率（%）',
    key: 'executeRate',
    align: 'center',
  },
  {
    title: '封顶值',
    key: 'capValue',
    align: 'center',
  },
]

const mchtNo = ref('')
const shopId = ref('')
onLoad((options: any) => {
  if (options.mchtNo) {
    mchtNo.value = options.mchtNo
  }
  if (options.shopId) {
    shopId.value = options.shopId
  }
})

const detail = ref({})
async function getDetail() {
  const pushData: any = {
    mchtNo: mchtNo.value,
  }
  if (shopId.value) {
    pushData.shopId = shopId.value
  }
  const { data } = await mcht.getDetail(pushData)
  if (data.status === 'success') {
    detail.value = data.data
  }
}

onMounted(() => {
  getDetail()
})

function clickTerminal(type) {
  let url = `/pages/my/merchant/terminal?type=${type}&mchtNo=${mchtNo.value}`
  if (shopId.value) {
    url += `&shopId=${shopId.value}`
  }
  uni.navigateTo({
    url,
  })
}

function masktring(str) {
  if (str.length <= 8) {
    return str
  }
  let start = str.slice(0, 4)
  let end = str.slice(-4)
  return start + ' **** ' + end
}
</script>

<template>
  <nut-cell-group v-if="detail.basicInfo" title="商户基本信息">
    <nut-cell title="商户名称" :desc="detail.basicInfo.mchtCnAbbr || '无'" />
    <nut-cell title="商户简称" :desc="detail.basicInfo.mchtName || '无'" />
    <option-select
      :model-value="detail.basicInfo.businessStatus"
      title="经营情况"
      readonly
      :options="businessStatusOptions"
    />
    <nut-cell title="注册地址" :desc="detail.basicInfo.regAddress || '无'" />
    <nut-cell title="经营地址" :desc="detail.basicInfo.businessAddress || '无'" />
    <nut-cell title="备用电话号码" :desc="detail.basicInfo.contactPhone || '无'" />
  </nut-cell-group>
  <nut-cell-group v-if="detail.ownerInfo" title="商户联系人信息">
    <option-select
      :model-value="detail.ownerInfo.idType"
      title="证件类型"
      readonly
      :options="idTypeOptions"
    />
    <nut-cell title="姓名" :desc="detail.ownerInfo.ownerName || '无'" />
    <nut-cell title="证件号码" :desc="detail.ownerInfo.idNumber || '无'" />
    <nut-cell title="发证日期" :desc="detail.ownerInfo.idStartDate || '无'" />
    <nut-cell title="到期日期" :desc="detail.ownerInfo.idEndDate || '无'" />
    <nut-cell title="手机号码" :desc="detail.ownerInfo.phoneNumber || '无'" />
  </nut-cell-group>
  <nut-cell-group v-if="detail.industryInfo" title="行业信息">
    <nut-cell title="行业类别" :desc="detail.industryInfo.industryCategory || '无'" />
    <nut-cell title="行业子类别" :desc="detail.industryInfo.subCategory || '无'" />
    <nut-cell title="行业细类" :desc="detail.industryInfo.detailedCategory || '无'" />
    <nut-cell title="银联MCC" :desc="detail.industryInfo.unionPayMcc || '无'" />
    <nut-cell title="微信类目ID" :desc="detail.industryInfo.weChatCategoryId || '无'" />
    <nut-cell title="支付宝类目ID" :desc="detail.industryInfo.aliPayCategoryId || '无'" />
    <nut-cell title="支付宝MCC" :desc="detail.industryInfo.aliPayMcc || '无'" />
  </nut-cell-group>
  <nut-cell-group v-if="detail.feeRateInfo" title="费率信息">
    <nut-table :columns="feeRateInfoColumns" :data="detail.feeRateInfo" />
  </nut-cell-group>
  <nut-cell-group v-if="detail.settlementInfo" title="结算账户信息">
    <nut-cell title="结算账户" :desc="masktring(detail.settlementInfo.accountNo) || '无'" />
    <nut-cell title="账户名称" :desc="detail.settlementInfo.accountName || '无'" />
    <nut-cell title="开户行名称" :desc="detail.settlementInfo.openingBank || '无'" />
  </nut-cell-group>
  <nut-cell-group v-if="detail.terminalInfo" title="设备信息">
    <nut-cell title="智能POS" :desc="`${detail.terminalInfo.smartPosCount}`" @click="clickTerminal('1')" />
    <nut-cell title="扫码枪" :desc="`${detail.terminalInfo.scannerGunCount}`" @click="clickTerminal('2')" />
    <nut-cell title="扫码盒" :desc="`${detail.terminalInfo.scannerBoxCount}`" @click="clickTerminal('3')" />
    <nut-cell title="云音箱" :desc="`${detail.terminalInfo.cloudSpeakerCount}`" @click="clickTerminal('4')" />
    <nut-cell title="二维码支付终端" :desc="`${detail.terminalInfo.qrPayBoxCount}`" @click="clickTerminal('6')" />
  </nut-cell-group>
  <nut-cell-group v-if="shopId && detail.shopInfo && detail.shopInfo.shopId" title="门店信息">
    <nut-cell title="门店号" :desc="detail.shopInfo.shopId || '无'" />
    <nut-cell title="门店名称" :desc="detail.shopInfo.shopName || '无'" />
    <nut-cell title="门店经营地址" :desc="detail.shopInfo.shopAddr || '无'" />
    <nut-cell title="店长姓名" :desc="detail.shopInfo.contactsNm || '无'" />
    <nut-cell title="店长电话" :desc="detail.shopInfo.contactsTel || '无'" />
    <option-select
      :model-value="detail.shopInfo.shopStatus"
      title="门店状态"
      readonly
      :options="shopStatusOptions"
    />
  </nut-cell-group>
</template>

<route lang="json">
{
  "layout": "home",
  "style": {
    "navigationBarTitleText": "商户详情",
    "backgroundColorContent": "#f1f1f1"
  }
}
</route>
